package com.anjiplus.template.gaea.business.modules.quartz.util;

import cn.hutool.db.Session;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.HashMap;
import java.util.Map;

public class DataSourceUtil {




    //GS 浪潮地址配置
    public static String IP= "smartfinance-b-test.norincocloud.com.cn";
//    private static final String mysqlJdbcUrl = "jdbc:mysql://127.0.0.1:13306/bm?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&useSSL=false&allowMultiQueries=true&serverTimezone=Asia/Shanghai";
//    private static final String mysqlUsername = "root";
//    private static final String mysqlPassword = "1q2w3e4r!";

    //ORACLE  配置
  //  private static final String oracleJdbcUrl = "jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST =47.93.220.141)(PORT = 1521)) (CONNECT_DATA = (SERVER = C##BIP2312) (SERVICE_NAME = ORCL)))";
//    private static final String oracleJdbcUrl = "jdbc:oracle:thin:@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST =47.93.220.141)(PORT = 1521)) (CONNECT_DATA = (SERVER = YCRHMID) (SERVICE_NAME = ORCL)))";
//    private static final String oracleUsername = "c##BIP2312";
//    private static final String oraclePassword = "BIP2312";

    private static DruidDataSource mysqlDataSource = null;
    private static Session mysqlSession = null;
    private static DruidDataSource oracleDataSource = null;
    private static Session oracleSession = null;
    public static String PORT= "8000";
    public static String ECC= "10003";
    public static String Bearer = "Bearer 35d8e4f7-15cb-7ab2-54ea-0bf2f107317c";
      JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);


    public static Map<String, Object> getGS(){
        Map<String, Object> gsMap = new HashMap<>();
        JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);
        String sql = "select * from gs_ecc where id = '1'";
        Map<String, Object> jdbcObjectMap = jdbcTemplate.queryForMap(sql);
        gsMap.put("IP",jdbcObjectMap.get("IP"));
        gsMap.put("PORT",jdbcObjectMap.get("PORT"));
        gsMap.put("ECC",jdbcObjectMap.get("ECC"));
        gsMap.put("Bearer",jdbcObjectMap.get("Bearer"));
        return gsMap;
    }



    /**
     * @return mysql����Դ
     */
    public static Session getMysqlSession() {
        JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);
        String sql = "select * from jdbc_util where code = '001'";
        Map<String, Object> jdbcObjectMap = jdbcTemplate.queryForMap(sql);
        mysqlDataSource = new DruidDataSource();
        mysqlDataSource.setUrl((String) jdbcObjectMap.get("url"));
        mysqlDataSource.setUsername((String) jdbcObjectMap.get("username"));
        mysqlDataSource.setPassword((String) jdbcObjectMap.get("password"));
        mysqlDataSource.setBreakAfterAcquireFailure(true);
        mysqlDataSource.setFailFast(true);
        mysqlDataSource.setMaxActive(500);
        mysqlDataSource.setTimeBetweenEvictionRunsMillis(30000);
        mysqlSession = Session.create(mysqlDataSource);
        return mysqlSession;
    }

    /**
     * �ر�mysql����
     */
    public static void closeMysqlSession() {
        if (null != mysqlDataSource) {
            mysqlDataSource.close();
        }
        if (null != mysqlSession) {
            mysqlSession.close();
        }
    }

    /**
     * @return oracle����Դ
     */
    public static Session getOracleSession() {
        JdbcTemplate jdbcTemplate = SpringUtil.getBean(JdbcTemplate.class);
        String sql = "select * from jdbc_util where code = '002'";
        Map<String, Object> jdbcObjectMap = jdbcTemplate.queryForMap(sql);
        oracleDataSource = new DruidDataSource();
        oracleDataSource.setUrl((String) jdbcObjectMap.get("url"));
        oracleDataSource.setUsername((String) jdbcObjectMap.get("username"));
        oracleDataSource.setPassword((String) jdbcObjectMap.get("password"));
        oracleDataSource.setBreakAfterAcquireFailure(true);
        oracleDataSource.setFailFast(true);
        oracleDataSource.setMaxActive(500);
        oracleDataSource.setTimeBetweenEvictionRunsMillis(30000);
        oracleSession = Session.create(oracleDataSource);
        return oracleSession;
    }

    /**
     * �ر�oracle����Դ����
     */
    public static void closeOracleSession() {
        if (null != oracleDataSource) {
            oracleDataSource.close();
        }
        if (null != oracleSession) {
            oracleSession.close();
        }
    }

}
